package Thread;

public class ThreadSum {
    private static int sum = 0;
    private static final Object lock = new Object();//使用两个线程对一个变量进行修改时，需要
                                                    //创建个锁变量来进行锁操作

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(()->{
            synchronized (lock){
                for (int i = 0; i < 10000; i++) {
                    sum++;
                }
            }
        });
        Thread thread2 = new Thread(()->{
            synchronized (lock){
                for (int i = 0; i < 10000; i++) {
                    sum++;
                }
            }
        });

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();


        System.out.println(sum);
    }
}
